package dao

import (
	"fmt"
	"time"

	"github.com/pelletier/go-toml"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var DB *gorm.DB

type User struct {
	Id        uint      `json:"id" gorm:"primaryKey"`
	Username  string    `json:"username" gorm:"unique;not null"`
	Password  string    `json:"password" gorm:"not null"`
	CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
}

type InviteCode struct {
	ClientId   string    `gorm:"primaryKey;column:client_id"` // 主键，数据库列名 client_id
	InviteCode string    `gorm:"not null;column:invite_code"` // 非空字段
	CreatedAt  time.Time `gorm:"autoCreateTime;column:created_at"`
	UpdatedAt  time.Time `gorm:"autoUpdateTime;column:updated_at"`
}

func InitDatabase(config *toml.Tree) {
	dsn := config.Get("env.DATABASE_URL").(string)
	fmt.Println("这里" + dsn)
	var err error
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("failed to connect database")
	}
	println("Database connected")
}
